//
//  HHNewsViewController.m
//  HHBroker
//
//  Created by He Wei on 10/3/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHNewsViewController.h"
#import "WMString-Utilities.h"
#import "HHNewsModel.h"
#import "HHNewsCell.h"
#import "HHWebViewController.h"

@interface HHNewsViewController () <NewsListCellDelegate>

@end

@implementation HHNewsViewController

#pragma mark - DeInit

- (void)dealloc
{

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新闻";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.footerView.hidden = YES;
}




#pragma mark - Delegate
#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseCell = @"reuseCell";
    HHNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseCell];
    if (cell == nil) {
        cell = [[HHNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseCell];
    }
    cell.newsArray = self.dataArray[indexPath.row];
    cell.delegate = self;
    return cell ;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.index = indexPath;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.0f;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *newsArray = self.dataArray[indexPath.row];
    return [HHNewsCell heightForModelCount:newsArray.count];

}

#pragma mark - NewsListCellDelegate
- (void)newsListCellDidClickedWithNewsID:(NSString *)newsID
{
    if (!newsID) {
        return;
    }
    HHWebViewController *web = [[HHWebViewController alloc] init];
    NSString *urlString = [NSString stringWithFormat:@"%@?news_id=%@", KUrlNewsDetail, newsID];
    web.title = @"新闻详情";
    web.urlString = urlString;
    [self.navigationController pushViewController:web animated:YES];
}

#pragma mark - Fun
- (void)loadTableViewDataSourceWithState:(HHRefreshState)state
{
    MBProgressHUD *hub;
    if (state == HHRefreshStateNormal) {
        hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    }
    NSString *time = [NSString currentTimeNumber];
    
    NSString *urlString = [NSString stringWithFormat:@"%@?p=%ld&release_time=%@", KUrlNewsList, (long)self.page, time];
    [HttpRequestManager getWithUrlString:urlString
                                 success:^(id response) {
                                     if (hub) {
                                         [hub hide:YES];
                                     }
                                     [self getJSONWithResponse:response tag:state];
                                     [self endRefreshing];
                                     
                                 }
                                 failure:^(NSError *error) {
                                     if (hub) {
                                         [hub hide:NO];
                                     }
                                     MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                                     hub.mode = MBProgressHUDModeText;
                                     hub.labelText = @"网络有点问题!";
                                     [hub hide:YES afterDelay:1];
                                     [self endRefreshing];
                                     
                                 } cache:NO];
}

- (void)getJSONWithResponse:(id)response tag:(NSInteger)tag
{
    NSInteger code = [[response objectForKey:@"code"] integerValue];
    if (code == 1) {
        id data = [response objectForKey:@"data"];
        if ([data isKindOfClass:[NSArray class]]) {
            NSArray *dataArray = (NSArray *)data;
            for (NSArray *array in dataArray) {
                NSMutableArray *listArray = [[NSMutableArray alloc] initWithCapacity:5];
                for (NSDictionary *dic in array) {
                    HHNewsModel *model = [[HHNewsModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [listArray addObject:model];
                }
                
                [self.dataArray addObject:listArray];
            }
        }
        if (self.dataArray.count == 0) {
            MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hub.mode = MBProgressHUDModeText;
            hub.labelText = @"暂无新闻";
            [hub hide:YES afterDelay:1];
        }
        if (self.dataArray.count < 10) {
        } else {
        }
        [self.tableView reloadData];
        // 第一次加载, 滚动到页面底部
        if (tag == 1) {
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataArray.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
        }
    }
}



























@end
